package com.xm.xmapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.xm.xmapp.LongRentOrderYuyueActivity;
import com.xm.xmapp.R;
import com.xm.xmapp.app.MyAppcation;
import com.xm.xmapp.base.BaseFragment;
import com.xm.xmapp.myinterface.DisssmissInterFace;
import com.xm.xmapp.myinterface.GetDataInterFace;
import com.xm.xmapp.request.bean.Get_network_car_genre_listRequest;
import com.xm.xmapp.response.bean.Get_network_car_genre_listResponse;
import com.xm.xmapp.response.bean.InterFaceLongRentOrderAble;
import com.xm.xmapp.response.bean.LongRentSubscribeBean;
import com.xm.xmapp.tools.Tools;


public class LongTimeSubscribeFragment extends BaseFragment implements OnClickListener {


    TextView tvCarGenreName;
    TextView tvOpeningHours;
    TextView tvTenancyType;
    RelativeLayout rlBtn;
    LinearLayout bottom;
    LinearLayout layout;
    ImageView ivPay;
    TextView tvBtn;
    private LongRentSubscribeBean bean;
    int time = -1;

    public LongTimeSubscribeFragment() {

    }

    public static LongTimeSubscribeFragment newInstance(InterFaceLongRentOrderAble bean) {
        LongTimeSubscribeFragment aa = new LongTimeSubscribeFragment();
        aa.setArguments(new Bundle());
        aa.setData(bean);
        return aa;
    }

    private void setData(InterFaceLongRentOrderAble bean2) {
        this.bean = (LongRentSubscribeBean) bean2;
    }


    public LongRentSubscribeBean getBean() {
        return bean;
    }

    public void setBean(InterFaceLongRentOrderAble bean) {

        this.bean = (LongRentSubscribeBean) bean;
        if (this.bean != null && view != null) {
            initData();
        }
    }


    @Override
    public int setContentView() {
        return R.layout.fragment_longrent_subscribe;
    }


    @Override
    public void initView() {

        tvCarGenreName = (TextView) findViewById(R.id.tv_car_genre_name);
        tvOpeningHours = (TextView) findViewById(R.id.tv_opening_hours);
        ivPay  = (ImageView) findViewById(R.id.iv_pay);
        tvTenancyType = (TextView) findViewById(R.id.tv_tenancy_type);
        rlBtn = (RelativeLayout) findViewById(R.id.rl_btn);
        layout = (LinearLayout) findViewById(R.id.layout);
        tvBtn = (TextView) findViewById(R.id.tv_btn);
        // 为fragment添加OnTouchListener监听器
        layout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // gesture.onTouchEvent(event);// 返回手势识别触发的事件
                return true;
            }
        });

    }

    @Override
    public void initData() {

        if (time == -1) {
        }

        tvCarGenreName.setText(this.bean.car_genre_name);
//        status.setText("待支付");
        if (this.bean.pay_finish_time != 0) {
            ivPay.setVisibility(View.INVISIBLE);
//            name.setText(this.bean.car_genre_name);
//            status.setText("待取车");
            tvBtn.setText("去看看");
        } else {
            ivPay.setVisibility(View.VISIBLE);
            tvBtn.setText("去支付");
        }
        tvOpeningHours.setText("取车时间:" + this.bean.take_date);

        if (bean.tenancy_type != 6 && bean.tenancy_type != 12)
            tvTenancyType.setText("用车期限:" + this.bean.tenancy_type + "个月");
        else {
            if (bean.tenancy_type == 6) {
                tvTenancyType.setText("用车期限:半年");
            } else {
                tvTenancyType.setText("用车期限:一年");
            }
        }

    }

    @Override
    public void initEvent() {
        rlBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_btn:
                getData();
                break;

            default:
                break;
        }
    }

    private void getData() {
        Get_network_car_genre_listRequest requestBean = new Get_network_car_genre_listRequest();
        requestBean.network_id = bean.subscribe_network_id;
        promptDialog.show();
        MyAppcation.getMyAppcation().getPostData(getActivity(), requestBean, new GetDataInterFace<String>() {

            @Override
            public void onResponse(String response) {
                try {
                    promptDialog.dismiss();
                    Get_network_car_genre_listResponse bean1 = JSON.parseObject(response,
                            Get_network_car_genre_listResponse.class);

                    if (bean1.carGenreList == null || bean1.carGenreList.size() == 0) {
                        Tools.showMessage("该网点暂无车型");
                    } else {
                        Intent intent = new Intent(MyAppcation.getMyAppcation(), LongRentOrderYuyueActivity.class);
                        intent.putExtra("bean", JSON.toJSONString(bean1));
                        intent.putExtra("JSONString", JSON.toJSONString(bean));
                        startActivity(intent);
                    }
                } catch (Exception e) {

                }
            }
        }, new DisssmissInterFace() {

            @Override
            public void putError(VolleyError error) {
                promptDialog.dismiss();
            }
        });
    }

}
